﻿// =================================================================== 
// 业务逻辑层(GDIT.BLL)
//====================================================================
// 文件：BBulletinArticle.cs
// 项目名称：学院网站
// 创建时间：2012-08-08
// 负责人：ChenZhiGang
//引用DevExpress.Data.Filtering，DevExpress.Xpo程序集
// 程序集介绍：公告文章操作逻辑
// ===================================================================
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using GDIT.Base.LogHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GDIT.Model;
using System.Data;

namespace GDIT.BLL
{
    public class BBulletinArticle
    {
        private Pattern.DALAbstractFactory dataFactory = DataProvider.defaultProvider;

        /// <summary>
        /// 管理员展示
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectBulletinByAdmin()
        {
            return dataFactory.DALBulletinArticleProvider.SelectAllBulletinByAdmin();
        }

        /// <summary>
        /// 查询可见字段为否的待审批记录
        /// </summary>
        /// <returns></returns>
        public IDataReader SelectIsVisibleByXZ()
        {
            return dataFactory.DALBulletinArticleProvider.SelectIsVisibleByXZ();
        }

        /// <summary>
        /// 新增加一篇公告
        /// </summary>
        /// <param name="bulletin">需要增加的公告对象</param>
        /// <returns>true：新增成功  false：新增失败</returns>
        public bool InsertBulletinArticle(Mtb_Users user)
        {
            Logger.Info(String.Format("新增公告的用户[{0}]", user.UserName));
            int result = dataFactory.DALDusersProvider.AddUserBulletinArticle(user);
            if (result == 1)
            {
                Logger.Info("Insert success");
                return true;
            }
            else
            {
                Logger.Info("Insert false");
                return false;
            }

        }

        /// <summary>
        /// 根据关键字查询并返回公告集合
        /// </summary>
        /// <param name="keyWord">输入的关键字</param>
        /// <returns></returns>
        public string[] SelectBulletinByKeyWord(string keyWord,int displaycount)
        {
             XPCollection xp = dataFactory.DALBulletinArticleProvider.SelectBulletinByKeyWord(keyWord);
             List<string> results = new List<string>(displaycount);
             foreach (Mtb_BulletinArticle word in xp)
             {
                 results.Add(word.Title);
             }
             return results.ToArray();
        }

        /// <summary>
        /// 删除一篇公告
        /// </summary>
        /// <param name="bulletin">需要删除的公告对象</param>
        /// <returns>1：删除成功  0：删除失败</returns>
        public bool DeleteBulletinArticle(int id)
        {
            Logger.Info(String.Format("要删除的公告ID[{0}]", id));
            int result = dataFactory.DALBulletinArticleProvider.DeleteBulletinArticle(id);
            if (result == 1)
            {
                Logger.Info("Delete success");
                return true;
            }
            else
            {
                Logger.Info("Delete false");
                return false;
            }
        }

        /// <summary>
        /// 更新一篇公告
        /// </summary>
        /// <param name="bulletin">需要更新的公告的对象</param>
        /// <returns>1：更新成功  0：更新失败</returns>
        public bool UpdateBulletinArticle(Mtb_BulletinArticle bulletin)
        {
            Logger.Info(String.Format("公告标题[{0}]", bulletin.Title));
            int result = dataFactory.DALBulletinArticleProvider.UpdateBulletinArticle(bulletin);
            if (result == 1)
            {
                Logger.Info("Insert success");
                return true;
            }
            else
            {
                Logger.Info("Insert false");
                return false;
            }
        }

        /// <summary>
        /// 增加公告的点击率
        /// </summary>
        public bool AddBulletinArticleCounter(int id)
        {
            Logger.Info(String.Format("需要增加公告id[{0}]", id));
            int result = dataFactory.DALBulletinArticleProvider.AddBulletinArticleCounter(id);
            if (result == 1)
            {
                Logger.Info("Update success");
                return true;
            }
            else
            {
                Logger.Info("Update false");
                return false;
            }
        }

        /// <summary>
        /// 查询所有非置顶，重点，内部的公告列表
        /// </summary>
        /// <returns>返回所有的公告列表</returns>
        public XPCollection SelectAllBulletinArticle()
        {
            return dataFactory.DALBulletinArticleProvider.SelectAllBulletinArticle();
        }

        /// <summary>
        /// 根据公告Id查询并返回公告单一记录
        /// </summary>
        /// <param name="id">需要查询的公告Id</param>
        /// <returns>返回公告单一记录</returns>
        public Mtb_BulletinArticle SelectBulletinArticleById(int id)
        {
            return dataFactory.DALBulletinArticleProvider.SelectBulletinArticleById(id);
        }

        /// <summary>
        /// 根据部门名称查询并返回该部门的所有公告列表
        /// </summary>
        /// <param name="name">需要查询的部门名称</param>
        /// <returns>返回该部门的公告列表</returns>
        public XPCollection SelectBulletinArticleByDepartment(string departmentName)
        {
            return dataFactory.DALBulletinArticleProvider.SelectBulletinArticleByDepartment(departmentName);
        }

        /// <summary>
        /// 显示置顶公告
        /// </summary>
        /// <returns>返回置顶公告列表</returns>
        public XPCollection SelectBulletinArticleByTop()
        {
            return dataFactory.DALBulletinArticleProvider.SelectBulletinArticleByTop();
        }

        /// <summary>
        /// 显示重点公告
        /// </summary>
        /// <returns>返回重点公告列表</returns>
        public XPCollection SelectBulletinArticleByImportant()
        {
            return dataFactory.DALBulletinArticleProvider.SelectBulletinArticleByImportant();
        }

        /// <summary>
        /// 显示内部置顶公告
        /// </summary>
        /// <returns>返回置顶公告列表</returns>
        public XPCollection SelectInternalBulletinArticleByTop()
        {
            return dataFactory.DALBulletinArticleProvider.SelectInternalBulletinArticleByTop();
        }

        /// <summary>
        /// 显示内部重点公告
        /// </summary>
        /// <returns>返回重点公告列表</returns>
        public XPCollection SelectInternalBulletinArticleByImportant()
        {
            return dataFactory.DALBulletinArticleProvider.SelectInternalBulletinArticleByImportant();
        }

        /// <summary>
        /// 设置公告为内部显示
        /// </summary>
        /// <returns>True：更新成功  False：更新失败</returns>
        public bool UpdateBulletinArticleByInternal(Mtb_BulletinArticle bulletin)
        {
            Logger.Info(String.Format("要设置内部的公告标题[{0}]", bulletin.Title));
            int result = dataFactory.DALBulletinArticleProvider.UpdateBulletinArticleByInternal(bulletin);
            if (result == 1)
            {
                Logger.Info("Update success");
                return true;
            }
            else
            {
                Logger.Info("Update false");
                return false;
            }
        }

        /// <summary>
        /// 设置公告为重点显示
        /// </summary>
        /// <returns>True：更新成功  False：更新失败</returns>
        public bool UpdateBulletinArticleByImportant(Mtb_BulletinArticle bulletin)
        {
            Logger.Info(String.Format("要设置重点的公告标题[{0}]", bulletin.Title));
            int result = dataFactory.DALBulletinArticleProvider.UpdateBulletinArticleByImportant(bulletin);
            if (result == 1)
            {
                Logger.Info("Update success");
                return true;
            }
            else
            {
                Logger.Info("Update false");
                return false;
            }
        }

        /// <summary>
        /// 设置公告为置顶显示
        /// </summary>
        /// <returns>True：更新成功  False：更新失败</returns>
        public bool UpdateBulletinArticleByTop(Mtb_BulletinArticle bulletin)
        {
            Logger.Info(String.Format("要设置置顶的公告标题[{0}]", bulletin.Title));
            int result = dataFactory.DALBulletinArticleProvider.UpdateBulletinArticleByTop(bulletin);
            if (result == 1)
            {
                Logger.Info("Update success");
                return true;
            }
            else
            {
                Logger.Info("Update false");
                return false;
            }
        }

        /// <summary>
        /// 显示首页的公告内容
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectBulletinByIndex()
        {
            XPCollection result = new XPCollection(typeof(Mtb_BulletinArticle));
            XPCollection xp = dataFactory.DALBulletinArticleProvider.SelectBulletinArticleByTopAndImportant();
            for (int i = 0; i <= 5; i++)
            {
                result.Add(xp[i]);
            }
            return result;
        }

        /// <summary>
        /// 显示内部首页的公告内容
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectInternalBulletinByIndex()
        {
            XPCollection result = new XPCollection(typeof(Mtb_BulletinArticle));
            XPCollection xp = dataFactory.DALBulletinArticleProvider.SelectInternalBulletinArticleByTopAndImportant();
            for (int i = 0; i <= 5; i++)
            {
                result.Add(xp[i]);
            }
            return result;
        }

        /// <summary>
        /// 显示公告内容
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectBulletinArticleByTopAndImportant()
        {
            return dataFactory.DALBulletinArticleProvider.SelectBulletinArticleByTopAndImportant();
        }

        /// <summary>
        /// 显示内部公告内容
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectInternalBulletinArticleByTopAndImportant()
        {
            return dataFactory.DALBulletinArticleProvider.SelectInternalBulletinArticleByTopAndImportant();
        }
    }
}
